<!DOCTYPE html>
<html lang="zh-CN" data-theme="light">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="generator" content="VuePress 2.0.0-beta.51" />
    <meta name="theme" content="VuePress Theme Hope" />
    <meta property="og:url" content="https://pro.autojs.org/docs/zh/v8/threads.html">
    <meta property="og:site_name" content="Auto.js Pro文档">
    <meta property="og:title" content="threads - 多线程">
    <meta property="og:type" content="article">
    <meta property="og:updated_time" content="2022-10-22T07:36:15.000Z">
    <meta property="og:locale" content="zh-CN">
    <meta property="og:locale:alternate" content="en-US">
    <meta property="article:modified_time" content="2022-10-22T07:36:15.000Z">
    <link rel="alternate" hreflang="en-us" href="https://pro.autojs.org/docs/en/zh/v8/threads.html">
    <link rel="icon" href="https://pro.autojs.org/docs/favicon.ico">
    <link rel="manifest" href="https://pro.autojs.org/docs/manifest.webmanifest" crossorigin="use-credentials">
    <meta name="theme-color" content="#46bd87">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
    <title>threads - 多线程 | Auto.js Pro文档</title>
    <meta name="description" content="支持Node.js的自动化工作流JavaScript IDE">
    <style>
        :root {
            --bg-color: #fff;
        }

        html[data-theme="dark"] {
            --bg-color: #1d2025;
        }

        html,
        body {
            background-color: var(--bg-color);
        }
    </style>
    <script>
        const userMode = localStorage.getItem("vuepress-theme-hope-scheme");
        const systemDarkMode =
            window.matchMedia &&
            window.matchMedia("(prefers-color-scheme: dark)").matches;

        if (userMode === "dark" || (userMode !== "light" && systemDarkMode)) {
            document.querySelector("html").setAttribute("data-theme", "dark");
        }
    </script>
    <link rel="stylesheet" href="../../assets/style.ae9cb182.css">
    <link rel="modulepreload" href="../../assets/app.6aa2b576.js">
    <link rel="modulepreload" href="../../assets/threads.html.fed6706c.js">
    <link rel="modulepreload" href="../../assets/_plugin-vue_export-helper.cdc0426e.js">
    <link rel="modulepreload" href="../../assets/threads.html.0bfa2c6d.js">
</head>

<body>


    <!-- Copyright � 2008. Spidersoft Ltd -->
    <style>
        A.applink:hover {
            border: 2px dotted #DCE6F4;
            padding: 2px;
            background-color: #ffff00;
            color: green;
            text-decoration: none
        }

        A.applink {
            border: 2px dotted #DCE6F4;
            padding: 2px;
            color: #2F5BFF;
            background: transparent;
            text-decoration: none
        }

        A.info {
            color: #2F5BFF;
            background: transparent;
            text-decoration: none
        }

        A.info:hover {
            color: green;
            background: transparent;
            text-decoration: underline
        }
    </style>


    <div id="app"><!--[--><!--[--><!--[--><span tabindex="-1"></span><a href="#main-content"
            class="skip-link sr-only">Skip to content</a><!--]-->
        <div class="theme-container has-toc"><!--[--><!--[-->
            <header class="navbar">
                <div class="navbar-left"><button class="toggle-sidebar-button" title="Toggle Sidebar"><span
                            class="icon"></span></button><!----><a href="../../docs.html" class="brand"><img
                            class="logo" src="../../logo.png" alt="Auto.js Pro文档"><!----><span
                            class="site-name hide-in-pad">Auto.js Pro文档</span></a><!----></div>
                <div class="navbar-center"><!---->
                    <nav class="nav-links">
                        <div class="nav-item hide-in-mobile"><a href="../../docs.html" class="nav-link"
                                aria-label="Auto.js Pro"><span class="icon iconfont icon-home"></span>Auto.js
                                Pro<!----></a></div>
                        <div class="nav-item hide-in-mobile"><a href="../guide/guide.html" class="nav-link"
                                aria-label="指南"><span class="icon iconfont icon-creative"></span>指南<!----></a></div>
                        <div class="nav-item hide-in-mobile"><a href="v8.html" class="nav-link active"
                                aria-label="第一代API文档"><span class="icon iconfont icon-note"></span>第一代API文档<!----></a>
                        </div>
                        <div class="nav-item hide-in-mobile"><a href="../v9/v9.html" class="nav-link"
                                aria-label="第二代API文档"><span class="icon iconfont icon-note"></span>第二代API文档<!----></a>
                        </div>
                        <div class="nav-item hide-in-mobile">
                            <div class="dropdown-wrapper"><button class="dropdown-title" type="button"
                                    aria-label="官网与相关链接"><span class="title"><span
                                            class="icon iconfont icon-discover"></span>官网与相关链接</span><span
                                        class="arrow"></span>
                                    <ul class="nav-dropdown">
                                        <li class="dropdown-item"><a href="../../../pro.autojs.html"
                                                rel="noopener noreferrer" target="_blank" aria-label="官网与软件下载"
                                                class="nav-link"><!---->官网与软件下载<span><svg class="external-link-icon"
                                                        xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
                                                        focusable="false" x="0px" y="0px" viewBox="0 0 100 100"
                                                        width="15" height="15">
                                                        <path fill="currentColor"
                                                            d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                                                        </path>
                                                        <polygon fill="currentColor"
                                                            points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                                                        </polygon>
                                                    </svg><span class="external-link-icon-sr-only">open in new
                                                        window</span></span><!----></a></li>
                                        <li class="dropdown-item"><a href="https://blog.autojs.org/"
                                                rel="noopener noreferrer" target="_blank" aria-label="官方博客"
                                                class="nav-link"><!---->官方博客<span><svg class="external-link-icon"
                                                        xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
                                                        focusable="false" x="0px" y="0px" viewBox="0 0 100 100"
                                                        width="15" height="15">
                                                        <path fill="currentColor"
                                                            d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                                                        </path>
                                                        <polygon fill="currentColor"
                                                            points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                                                        </polygon>
                                                    </svg><span class="external-link-icon-sr-only">open in new
                                                        window</span></span><!----></a></li>
                                        <li class="dropdown-item"><a href="https://pd.qq.com/s/gosos5"
                                                rel="noopener noreferrer" target="_blank" aria-label="官方频道"
                                                class="nav-link"><!---->官方频道<span><svg class="external-link-icon"
                                                        xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
                                                        focusable="false" x="0px" y="0px" viewBox="0 0 100 100"
                                                        width="15" height="15">
                                                        <path fill="currentColor"
                                                            d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                                                        </path>
                                                        <polygon fill="currentColor"
                                                            points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                                                        </polygon>
                                                    </svg><span class="external-link-icon-sr-only">open in new
                                                        window</span></span><!----></a></li>
                                    </ul>
                                </button></div>
                        </div>
                    </nav><!---->
                </div>
                <div class="navbar-right"><!---->
                    <div class="nav-item">
                        <div class="dropdown-wrapper i18n-dropdown"><button class="dropdown-title" type="button"
                                aria-label="选择语言"><!--[--><svg xmlns="http://www.w3.org/2000/svg" class="icon i18n-icon"
                                    viewBox="0 0 1024 1024" fill="currentColor" aria-label="i18n icon"
                                    style="width:1rem;height:1rem;vertical-align:middle;">
                                    <path
                                        d="M379.392 460.8 494.08 575.488l-42.496 102.4L307.2 532.48 138.24 701.44l-71.68-72.704L234.496 460.8l-45.056-45.056c-27.136-27.136-51.2-66.56-66.56-108.544h112.64c7.68 14.336 16.896 27.136 26.112 35.84l45.568 46.08 45.056-45.056C382.976 312.32 409.6 247.808 409.6 204.8H0V102.4h256V0h102.4v102.4h256v102.4H512c0 70.144-37.888 161.28-87.04 210.944L378.88 460.8zM576 870.4 512 1024H409.6l256-614.4H768l256 614.4H921.6l-64-153.6H576zM618.496 768h196.608L716.8 532.48 618.496 768z">
                                    </path>
                                </svg><!--]--><span class="arrow"></span>
                                <ul class="nav-dropdown">
                                    <li class="dropdown-item"><a href="../../en/en.html" class="nav-link"
                                            aria-label="English"><!---->English<!----></a></li>
                                    <li class="dropdown-item"><a aria-current="page" href="threads.html"
                                            class="router-link-active router-link-exact-active nav-link active"
                                            aria-label="简体中文"><!---->简体中文<!----></a></li>
                                </ul>
                            </button></div>
                    </div><!---->
                    <div class="nav-item hide-in-mobile"><button id="appearance-switch"><svg
                                xmlns="http://www.w3.org/2000/svg" class="icon auto-icon" viewBox="0 0 1024 1024"
                                fill="currentColor" aria-label="auto icon" style="display:block;">
                                <path
                                    d="M512 992C246.92 992 32 777.08 32 512S246.92 32 512 32s480 214.92 480 480-214.92 480-480 480zm0-840c-198.78 0-360 161.22-360 360 0 198.84 161.22 360 360 360s360-161.16 360-360c0-198.78-161.22-360-360-360zm0 660V212c165.72 0 300 134.34 300 300 0 165.72-134.28 300-300 300z">
                                </path>
                            </svg><svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" viewBox="0 0 1024 1024"
                                fill="currentColor" aria-label="dark icon" style="display:none;">
                                <path
                                    d="M524.8 938.667h-4.267a439.893 439.893 0 0 1-313.173-134.4 446.293 446.293 0 0 1-11.093-597.334A432.213 432.213 0 0 1 366.933 90.027a42.667 42.667 0 0 1 45.227 9.386 42.667 42.667 0 0 1 10.24 42.667 358.4 358.4 0 0 0 82.773 375.893 361.387 361.387 0 0 0 376.747 82.774 42.667 42.667 0 0 1 54.187 55.04 433.493 433.493 0 0 1-99.84 154.88 438.613 438.613 0 0 1-311.467 128z">
                                </path>
                            </svg><svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon"
                                viewBox="0 0 1024 1024" fill="currentColor" aria-label="light icon"
                                style="display:none;">
                                <path
                                    d="M952 552h-80a40 40 0 0 1 0-80h80a40 40 0 0 1 0 80zM801.88 280.08a41 41 0 0 1-57.96-57.96l57.96-58a41.04 41.04 0 0 1 58 58l-58 57.96zM512 752a240 240 0 1 1 0-480 240 240 0 0 1 0 480zm0-560a40 40 0 0 1-40-40V72a40 40 0 0 1 80 0v80a40 40 0 0 1-40 40zm-289.88 88.08-58-57.96a41.04 41.04 0 0 1 58-58l57.96 58a41 41 0 0 1-57.96 57.96zM192 512a40 40 0 0 1-40 40H72a40 40 0 0 1 0-80h80a40 40 0 0 1 40 40zm30.12 231.92a41 41 0 0 1 57.96 57.96l-57.96 58a41.04 41.04 0 0 1-58-58l58-57.96zM512 832a40 40 0 0 1 40 40v80a40 40 0 0 1-80 0v-80a40 40 0 0 1 40-40zm289.88-88.08 58 57.96a41.04 41.04 0 0 1-58 58l-57.96-58a41 41 0 0 1 57.96-57.96z">
                                </path>
                            </svg></button></div>
                    <div class="search-box" role="search" data-v-f3875574><input value="" aria-label="Search" class=""
                            placeholder="Search" autocomplete="off" spellcheck="false" data-v-f3875574><!----></div>
                    <!----><button class="toggle-navbar-button" aria-label="Toggle Navbar" aria-expanded="false"
                        aria-controls="nav-screen"><span class="button-container"><span class="button-top"></span><span
                                class="button-middle"></span><span class="button-bottom"></span></span></button>
                </div>
            </header><!----><!--]--><!---->
            <div class="toggle-sidebar-wrapper"><span class="arrow left"></span></div>
            <aside class="sidebar"><!--[--><!----><!--]-->
                <ul class="sidebar-links">
                    <li>
                        <section class="sidebar-group">
                            <p class="sidebar-heading"><!----><span class="title">指南</span><!----></p>
                            <ul class="sidebar-links">
                                <li><!--[--><a href="v8.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="阅读须知"><!---->阅读须知<!----></a>
                                    <ul class="sidebar-sub-headers"></ul><!--]-->
                                </li>
                            </ul>
                        </section>
                    </li>
                    <li>
                        <section class="sidebar-group">
                            <p class="sidebar-heading"><!----><span class="title">API文档</span><!----></p>
                            <ul class="sidebar-links">
                              <li>
                                <section class="sidebar-group"><button class="sidebar-heading clickable"><!----><span
                                      class="title">自动化 -
                                      控件操作</span><span class="arrow down"></span></button>
                                  <ul class="sidebar-links">
                                    <li><!--[--><a href="./automator/"
                                        class="router-link-active router-link-exact-active nav-link sidebar-link sidebar-page"
                                        aria-label="入门介绍" aria-current="page"><!---->入门介绍<!----></a>
                                      <ul class="sidebar-sub-headers"></ul><!--]-->
                                    </li>
                                    <li><!--[--><a href="./automator/selector.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="选择器"><!---->选择器<!----></a>
                                      <ul class="sidebar-sub-headers"></ul><!--]-->
                                    </li>
                                    <li><!--[--><a href="./automator/uiobject.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="控件和控件集合"><!---->控件和控件集合<!----></a>
                                      <ul class="sidebar-sub-headers"></ul><!--]-->
                                    </li>
                                    <li><!--[--><a href="./automator/api.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="automator与其他函数"><!---->automator与其他函数<!----></a>
                                      <ul class="sidebar-sub-headers"></ul><!--]-->
                                    </li>
                                  </ul>
                                </section>
                              </li>
                              <li><!--[--><a href="./coordinatesBasedAutomation.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="自动化 - 坐标操作"><!---->自动化 -
                                  坐标操作<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./app.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="app - 应用"><!---->app -
                                  应用<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./base64.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="base64"><!---->base64<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./colors.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="colors - 颜色"><!---->colors - 颜色<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./canvas.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="canvas - 画布"><!---->canvas - 画布<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./console.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="console - 控制台"><!---->console - 控制台<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./crypto.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="crypto - 加解密与消息摘要"><!---->crypto - 加解密与消息摘要<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./debug.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="debug - 调试工具"><!---->debug - 调试工具<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./device.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="device - 设备"><!---->device - 设备<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./dialogs.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="dialogs - 对话框"><!---->dialogs - 对话框<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./engines.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="engines - 脚本引擎"><!---->engines - 脚本引擎<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./events.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="events - 事件与监听"><!---->events - 事件与监听<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./floaty.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="floaty - 悬浮窗"><!---->floaty - 悬浮窗<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./files.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="files - 文件系统"><!---->files - 文件系统<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./globals.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="globals - 全局变量与函数"><!---->globals - 全局变量与函数<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./http.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="http - HTTP网络请求"><!---->http - HTTP网络请求<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./images.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="images - 图片处理"><!---->images - 图片处理<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./keys.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="keys - 按键模拟"><!---->keys
                                  - 按键模拟<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./media.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="media - 多媒体"><!---->media
                                  - 多媒体<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./modules.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="module - 模块"><!---->module - 模块<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./ocr.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="ocr - 文字识别"><!---->ocr -
                                  文字识别<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./plugins.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="plugins - 插件"><!---->plugins - 插件<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./powerManager.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="power_manager - 电源管理"><!---->power_manager - 电源管理<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./sensors.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="sensors - 传感器"><!---->sensors - 传感器<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./shell.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="shell - Shell命令"><!---->shell - Shell命令<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./storages.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="storages - 本地存储"><!---->storages - 本地存储<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./settings.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="settings - 设置"><!---->settings - 设置<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./threads.html" class="nav-link sidebar-link sidebar-page active"
                                  aria-label="threads - 多线程"><!---->threads - 多线程<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./timers.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="timers - 定时器"><!---->timers - 定时器<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./timedTasks.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="work_manager - 定时与任务"><!---->work_manager - 定时与任务<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li>
                                <section class="sidebar-group"><button class="sidebar-heading clickable"><!----><span
                                      class="title">ui - 用户界面</span><span class="arrow down"></span></button>
                                  <ul class="sidebar-links">
                                    <li><a href="/docs/zh/v8/ui/ui.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="ui入门介绍"><!---->ui入门介绍<!----></a>
                                      <ul class="sidebar-sub-headers"></ul>
                                    </li>
                                    <li><a href="/docs/zh/v8/ui/basic.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="基础布局与控件"><!---->基础布局与控件<!----></a>
                                      <ul class="sidebar-sub-headers"></ul>
                                    </li>
                                    <li><a href="/docs/zh/v8/ui/advanced.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="其他布局与控件"><!---->其他布局与控件<!----></a>
                                      <ul class="sidebar-sub-headers"></ul>
                                    </li>
                                    <li><a href="/docs/zh/v8/ui/api.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="api文档"><!---->api文档<!----></a>
                                      <ul class="sidebar-sub-headers"></ul>
                                    </li>
                                    <li><a href="/docs/zh/v8/ui/customWidget.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="自定义控件"><!---->自定义控件<!----></a>
                                      <ul class="sidebar-sub-headers"></ul>
                                    </li>
                                    <li><a aria-current="page" href="/docs/zh/v8/ui/webview.html"
                                        class="router-link-active router-link-exact-active nav-link sidebar-link sidebar-page"
                                        aria-label="使用Web编写界面"><!---->使用Web编写界面<!----></a>
                                      <ul class="sidebar-sub-headers"></ul>
                                    </li>
                                  </ul>
                                </section>
                              </li>
                              <li><!--[--><a href="./util.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="util - 工具"><!---->util -
                                  工具<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./webSocket.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="WebSocket"><!---->WebSocket<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                              <li><!--[--><a href="./zip.html" class="nav-link sidebar-link sidebar-page"
                                  aria-label="zip - 压缩与解压"><!---->zip -
                                  压缩与解压<!----></a>
                                <ul class="sidebar-sub-headers"></ul><!--]-->
                              </li>
                            </ul>
                          </section>
                    </li>
                    <li>
                        <section class="sidebar-group">
                            <p class="sidebar-heading"><!----><span class="title">进阶</span><!----></p>
                            <ul class="sidebar-links">
                                <li><!--[--><a href="scriptingJava.html" class="nav-link sidebar-link sidebar-page"
                                        aria-label="和Java交互"><!---->和Java交互<!----></a>
                                    <ul class="sidebar-sub-headers"></ul><!--]-->
                                </li>
                            </ul>
                        </section>
                    </li>
                </ul><!--[--><!----><!--]-->
            </aside><!--[-->
            <main class="page" id="main-content"><!--[--><!---->
                <nav class="breadcrumb disable"></nav>
                <div class="page-title">
                    <h1><!---->threads - 多线程</h1>
                    <div class="page-info"><span class="date-info" aria-label="写作日期📅" data-balloon-pos="down"><svg
                                xmlns="http://www.w3.org/2000/svg" class="icon calendar-icon" viewBox="0 0 1024 1024"
                                fill="currentColor" aria-label="calendar icon">
                                <path
                                    d="M716.4 110.137c0-18.753-14.72-33.473-33.472-33.473-18.753 0-33.473 14.72-33.473 33.473v33.473h66.993v-33.473zm-334.87 0c0-18.753-14.72-33.473-33.473-33.473s-33.52 14.72-33.52 33.473v33.473h66.993v-33.473zm468.81 33.52H716.4v100.465c0 18.753-14.72 33.473-33.472 33.473a33.145 33.145 0 01-33.473-33.473V143.657H381.53v100.465c0 18.753-14.72 33.473-33.473 33.473a33.145 33.145 0 01-33.473-33.473V143.657H180.6A134.314 134.314 0 0046.66 277.595v535.756A134.314 134.314 0 00180.6 947.289h669.74a134.36 134.36 0 00133.94-133.938V277.595a134.314 134.314 0 00-133.94-133.938zm33.473 267.877H147.126a33.145 33.145 0 01-33.473-33.473c0-18.752 14.72-33.473 33.473-33.473h736.687c18.752 0 33.472 14.72 33.472 33.473a33.145 33.145 0 01-33.472 33.473z">
                                </path>
                            </svg><span>2022年10月22日</span>
                            <meta property="datePublished" content="2022-10-22T07:36:15.000Z">
                        </span></div>
                    <hr>
                </div>
                <div class="toc-place-holder">
                    <aside id="toc">
                        <div class="toc-header">此页内容</div>
                        <div class="toc-wrapper">
                            <ul class="toc-list"><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#threads-start-action"
                                        class="router-link-active router-link-exact-active toc-link level2">threads.start(action)</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#threads-shutdownall"
                                        class="router-link-active router-link-exact-active toc-link level2">threads.shutDownAll()</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#threads-currentthread"
                                        class="router-link-active router-link-exact-active toc-link level2">threads.currentThread()</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#threads-disposable"
                                        class="router-link-active router-link-exact-active toc-link level2">threads.disposable()</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#threads-atomic-initialvalue"
                                        class="router-link-active router-link-exact-active toc-link level2">threads.atomic([initialValue])</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#threads-lock"
                                        class="router-link-active router-link-exact-active toc-link level2">threads.lock()</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#thread-interrupt"
                                        class="router-link-active router-link-exact-active toc-link level2">Thread.interrupt()</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#thread-join-timeout"
                                        class="router-link-active router-link-exact-active toc-link level2">Thread.join([timeout])</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#isalive"
                                        class="router-link-active router-link-exact-active toc-link level2">isAlive()</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#waitfor"
                                        class="router-link-active router-link-exact-active toc-link level2">waitFor()</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#thread-settimeout-callback-delay-args"
                                        class="router-link-active router-link-exact-active toc-link level2">Thread.setTimeout(callback,
                                        delay[, ...args])</a></li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#thread-setinterval-callback-delay-args"
                                        class="router-link-active router-link-exact-active toc-link level2">Thread.setInterval(callback,
                                        delay[, ...args])</a></li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#thread-setimmediate-callback-args"
                                        class="router-link-active router-link-exact-active toc-link level2">Thread.setImmediate(callback[,
                                        ...args])</a></li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#thread-clearinterval-id"
                                        class="router-link-active router-link-exact-active toc-link level2">Thread.clearInterval(id)</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#thread-cleartimeout-id"
                                        class="router-link-active router-link-exact-active toc-link level2">Thread.clearTimeout(id)</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#thread-clearimmediate-id"
                                        class="router-link-active router-link-exact-active toc-link level2">Thread.clearImmediate(id)</a>
                                </li><!----><!--]--><!--[-->
                                <li class="toc-item"><a aria-current="page"
                                        href="https://pro.autojs.org/docs/zh/v8/threads.html#sync-func"
                                        class="router-link-active router-link-exact-active toc-link level2">sync(func)</a>
                                </li><!----><!--]-->
                            </ul>
                        </div>
                    </aside>
                </div><!---->
                <div class="theme-hope-content">
                    <h1 id="threads-多线程" tabindex="-1"><a class="header-anchor" href="#threads-多线程"
                            aria-hidden="true">#</a> threads - 多线程</h1>
                    <blockquote>
                        <p>Stability: 1 - Experiment</p>
                    </blockquote>
                    <p>threads模块提供了多线程支持，可以启动新线程来运行脚本。</p>
                    <p>脚本主线程会等待所有子线程执行完成后才停止执行，因此如果子线程中有死循环，请在必要的时候调用<code><span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>来直接停止脚本或<code>threads<span class="token punctuation">.</span><span class="token function">shutDownAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>来停止所有子线程。
                    </p>
                    <p>通过<code>threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>启动的所有线程会在脚本被强制停止时自动停止。
                    </p>
                    <p>由于JavaScript自身没有多线程的支持，因此您可能会遇到意料之外的问题。</p>
                    <h2 id="threads-start-action" tabindex="-1"><a class="header-anchor" href="#threads-start-action"
                            aria-hidden="true">#</a> threads.start(action)</h2>
                    <ul>
                        <li><code><span class="token property">action</span></code> {Function} 要在新线程执行的函数</li>
                        <li>返回 {<a href="#threads_thread">Thread</a>}</li>
                    </ul>
                    <p>启动一个新线程并执行action。</p>
                    <p>例如:</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code>threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//在新线程执行的代码</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;子线程&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;脚本主线程&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>通过该函数返回的<a href="#threads_thread">Thread</a>对象可以获取该线程的状态，控制该线程的运行中。例如:</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> thread <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;子线程&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//停止线程执行</span>
thread<span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>更多信息参见<a href="#threads_thread">Thread</a>。</p>
                    <h2 id="threads-shutdownall" tabindex="-1"><a class="header-anchor" href="#threads-shutdownall"
                            aria-hidden="true">#</a> threads.shutDownAll()</h2>
                    <p>停止所有通过<code>threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>启动的子线程。
                    </p>
                    <h2 id="threads-currentthread" tabindex="-1"><a class="header-anchor" href="#threads-currentthread"
                            aria-hidden="true">#</a> threads.currentThread()</h2>
                    <ul>
                        <li>返回 {<a href="#threads_thread">Thread</a>}</li>
                    </ul>
                    <p>返回当前线程。</p>
                    <h2 id="threads-disposable" tabindex="-1"><a class="header-anchor" href="#threads-disposable"
                            aria-hidden="true">#</a> threads.disposable()</h2>
                    <ul>
                        <li>返回 {<a href="#threads_disposable">Disposable</a>}</li>
                    </ul>
                    <p>新建一个Disposable对象，用于等待另一个线程的某个一次性结果。更多信息参见<a
                            href="#threads_%E7%BA%BF%E7%A8%8B%E9%80%9A%E4%BF%A1">线程通信</a>以及<a
                            href="#threads_disposable">Disposable</a>。</p>
                    <h2 id="threads-atomic-initialvalue" tabindex="-1"><a class="header-anchor"
                            href="#threads-atomic-initialvalue" aria-hidden="true">#</a> threads.atomic([initialValue])
                    </h2>
                    <ul>
                        <li><code><span class="token property">initialValue</span></code> {number} 初始整数值，默认为0</li>
                        <li>返回 {<a
                                href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html"
                                target="_blank" rel="noopener noreferrer">AtomicLong<span><svg
                                        class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
                                        focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15">
                                        <path fill="currentColor"
                                            d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                                        </path>
                                        <polygon fill="currentColor"
                                            points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                                        </polygon>
                                    </svg><span class="external-link-icon-sr-only">open in new window</span></span></a>}
                        </li>
                    </ul>
                    <p>新建一个整数原子变量。更多信息参见<a href="#threads_%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8">线程安全</a>以及<a
                            href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html"
                            target="_blank" rel="noopener noreferrer">AtomicLong<span><svg class="external-link-icon"
                                    xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px"
                                    y="0px" viewBox="0 0 100 100" width="15" height="15">
                                    <path fill="currentColor"
                                        d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                                    </path>
                                    <polygon fill="currentColor"
                                        points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                                    </polygon>
                                </svg><span class="external-link-icon-sr-only">open in new window</span></span></a>。</p>
                    <h2 id="threads-lock" tabindex="-1"><a class="header-anchor" href="#threads-lock"
                            aria-hidden="true">#</a> threads.lock()</h2>
                    <ul>
                        <li>返回 {<a
                                href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html"
                                target="_blank" rel="noopener noreferrer">ReentrantLock<span><svg
                                        class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
                                        focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15">
                                        <path fill="currentColor"
                                            d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                                        </path>
                                        <polygon fill="currentColor"
                                            points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                                        </polygon>
                                    </svg><span class="external-link-icon-sr-only">open in new window</span></span></a>}
                        </li>
                    </ul>
                    <p>新建一个可重入锁。更多信息参见<a href="#threads_%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8">线程安全</a>以及<a
                            href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html"
                            target="_blank" rel="noopener noreferrer">ReentrantLock<span><svg class="external-link-icon"
                                    xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px"
                                    y="0px" viewBox="0 0 100 100" width="15" height="15">
                                    <path fill="currentColor"
                                        d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                                    </path>
                                    <polygon fill="currentColor"
                                        points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                                    </polygon>
                                </svg><span class="external-link-icon-sr-only">open in new window</span></span></a>。</p>
                    <h1 id="thread" tabindex="-1"><a class="header-anchor" href="#thread" aria-hidden="true">#</a>
                        Thread</h1>
                    <p>线程对象，<code>threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>返回的对象，用于获取和控制线程的状态，与其他线程交互等。
                    </p>
                    <p>Thread对象提供了和timers模块一样的API，例如<code><span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>,
                        <code><span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>等，用于在该线程执行相应的定时回调，从而使线程之间可以直接交互。例如：
                    </p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> thread <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//在子线程执行的定时器</span>
    <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;子线程:&quot;</span> <span class="token operator">+</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程为主线程:&quot;</span> <span class="token operator">+</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//等待子线程启动</span>
thread<span class="token punctuation">.</span><span class="token function">waitFor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//在子线程执行的定时器</span>
thread<span class="token punctuation">.</span><span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//这段代码会在子线程执行</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程为子线程:&quot;</span> <span class="token operator">+</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">30</span> <span class="token operator">*</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <h2 id="thread-interrupt" tabindex="-1"><a class="header-anchor" href="#thread-interrupt"
                            aria-hidden="true">#</a> Thread.interrupt()</h2>
                    <p>中断线程运行。</p>
                    <h2 id="thread-join-timeout" tabindex="-1"><a class="header-anchor" href="#thread-join-timeout"
                            aria-hidden="true">#</a> Thread.join([timeout])</h2>
                    <ul>
                        <li><code><span class="token property">timeout</span></code> {number} 等待时间，单位毫秒</li>
                    </ul>
                    <p>等待线程执行完成。如果timeout为0，则会一直等待直至该线程执行完成；否则最多等待timeout毫秒的时间。</p>
                    <p>例如:</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token comment">//启动子线程计算1加到10000</span>
<span class="token keyword">var</span> thread <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        sum <span class="token operator">+=</span> i<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//等待该线程完成</span>
thread<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">toast</span><span class="token punctuation">(</span><span class="token string">&quot;sum = &quot;</span> <span class="token operator">+</span> sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <h2 id="isalive" tabindex="-1"><a class="header-anchor" href="#isalive" aria-hidden="true">#</a>
                        isAlive()</h2>
                    <ul>
                        <li>返回 {boolean}</li>
                    </ul>
                    <p>返回线程是否存活。如果线程仍未开始或已经结束，返回<code><span class="token boolean">false</span></code>;
                        如果线程已经开始或者正在运行中，返回<code><span class="token boolean">true</span></code>。</p>
                    <h2 id="waitfor" tabindex="-1"><a class="header-anchor" href="#waitfor" aria-hidden="true">#</a>
                        waitFor()</h2>
                    <p>等待线程开始执行。调用<code>threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>以后线程仍然需要一定时间才能开始执行，因此调用此函数会等待线程开始执行；如果线程已经处于执行状态则立即返回。
                    </p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> thread <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//do something</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">waitFor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//do something</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <h2 id="thread-settimeout-callback-delay-args" tabindex="-1"><a class="header-anchor"
                            href="#thread-settimeout-callback-delay-args" aria-hidden="true">#</a>
                        Thread.setTimeout(callback, delay[, ...args])</h2>
                    <p>参见<a href="https://pro.autojs.org/docs/zh/v8/timers.html#timers_settimeout_callback_delay_args"
                            class="">timers.setTimeout()</a>。</p>
                    <p>区别在于,
                        该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束，则抛出<code><span class="token property">IllegalStateException</span></code>。
                    </p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程(主线程):&quot;</span> <span class="token operator">+</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> thread <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//设置一个空的定时来保持线程的运行状态</span>
    <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程(子线程):&quot;</span> <span class="token operator">+</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <h2 id="thread-setinterval-callback-delay-args" tabindex="-1"><a class="header-anchor"
                            href="#thread-setinterval-callback-delay-args" aria-hidden="true">#</a>
                        Thread.setInterval(callback, delay[, ...args])</h2>
                    <p>参见<a href="https://pro.autojs.org/docs/zh/v8/timers.html#timers_setinterval_callback_delay_args"
                            class="">timers.setInterval()</a>。</p>
                    <p>区别在于,
                        该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束，则抛出<code><span class="token property">IllegalStateException</span></code>。
                    </p>
                    <h2 id="thread-setimmediate-callback-args" tabindex="-1"><a class="header-anchor"
                            href="#thread-setimmediate-callback-args" aria-hidden="true">#</a>
                        Thread.setImmediate(callback[, ...args])</h2>
                    <p>参见<a href="https://pro.autojs.org/docs/zh/v8/timers.html#timers_setimmediate_callback_delay_args"
                            class="">timers.setImmediate()</a>。</p>
                    <p>区别在于,
                        该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束，则抛出<code><span class="token property">IllegalStateException</span></code>。
                    </p>
                    <h2 id="thread-clearinterval-id" tabindex="-1"><a class="header-anchor"
                            href="#thread-clearinterval-id" aria-hidden="true">#</a> Thread.clearInterval(id)</h2>
                    <p>参见<a href="https://pro.autojs.org/docs/zh/v8/timers.html#timers_clearinterval_id"
                            class="">timers.clearInterval()</a>。</p>
                    <p>区别在于,
                        该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束，则抛出<code><span class="token property">IllegalStateException</span></code>。
                    </p>
                    <h2 id="thread-cleartimeout-id" tabindex="-1"><a class="header-anchor"
                            href="#thread-cleartimeout-id" aria-hidden="true">#</a> Thread.clearTimeout(id)</h2>
                    <p>参见<a href="https://pro.autojs.org/docs/zh/v8/timers.html#timers_cleartimeout_id"
                            class="">timers.clearTimeout()</a>。</p>
                    <p>区别在于,
                        该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束，则抛出<code><span class="token property">IllegalStateException</span></code>。
                    </p>
                    <h2 id="thread-clearimmediate-id" tabindex="-1"><a class="header-anchor"
                            href="#thread-clearimmediate-id" aria-hidden="true">#</a> Thread.clearImmediate(id)</h2>
                    <p>参见<a href="https://pro.autojs.org/docs/zh/v8/timers.html#timers_clearimmediate_id"
                            class="">timers.clearImmediate()</a>。</p>
                    <p>区别在于,
                        该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束，则抛出<code><span class="token property">IllegalStateException</span></code>。
                    </p>
                    <h1 id="线程安全" tabindex="-1"><a class="header-anchor" href="#线程安全" aria-hidden="true">#</a> 线程安全</h1>
                    <p>线程安全问题是一个相对专业的编程问题，本章节只提供给有需要的用户。</p>
                    <p>引用维基百科的解释：</p>
                    <blockquote>
                        <p>线程安全是编程中的术语，指某个函数、函数库在多线程环境中被调用时，能够正确地处理多个线程之间的共享变量，使程序功能正确完成。</p>
                    </blockquote>
                    <p>在Auto.js中，线程间变量在符合JavaScript变量作用域规则的前提下是共享的，例如全局变量在所有线程都能访问，并且保证他们在所有线程的可见性。但是，不保证任何操作的原子性。例如经典的自增&quot;i++&quot;将不是原子性操作。
                    </p>
                    <p>Rhino和Auto.js提供了一些简单的设施来解决简单的线程安全问题，如锁<code>threads<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>,
                        函数同步锁<code><span class="token function">sync</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>,
                        整数原子变量<code>threads<span class="token punctuation">.</span><span class="token function">atomic</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>等。
                    </p>
                    <p>例如，对于多线程共享下的整数的自增操作(自增操作会导致问题，是因为自增操作实际上为<code>i <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">1</span></code>，也就是先读取i的值,
                        把他加1, 再赋值给i,
                        如果两个线程同时进行自增操作，可能出现i的值只增加了1的情况)，应该使用<code>threads<span class="token punctuation">.</span><span class="token function">atomic</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>函数来新建一个整数原子变量，或者使用锁<code>threads<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>来保证操作的原子性，或者用<code><span class="token function">sync</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>来增加同步锁。
                    </p>
                    <p>线程不安全的代码如下：</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">log</span><span class="token punctuation">(</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>此段代码运行后打开日志，可以看到日志中有重复的值出现。</p>
                    <p>使用<code>threads<span class="token punctuation">.</span><span class="token function">atomic</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>的线程安全的代码如下:
                    </p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token comment">//atomic返回的对象保证了自增的原子性</span>
<span class="token keyword">var</span> i <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">atomic</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">.</span><span class="token function">getAndIncrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">.</span><span class="token function">getAndIncrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>或者:</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token comment">//锁保证了操作的原子性</span>
<span class="token keyword">var</span> lock <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">log</span><span class="token punctuation">(</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">log</span><span class="token punctuation">(</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>或者:</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token comment">//sync函数会把里面的函数加上同步锁，使得在同一时刻最多只能有一个线程执行这个函数</span>
<span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> getAndIncrement <span class="token operator">=</span> <span class="token function">sync</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> i<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">log</span><span class="token punctuation">(</span><span class="token function">getAndIncrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token function">getAndIncrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>另外，数组Array不是线程安全的，如果有这种复杂的需求，请用Android和Java相关API来实现。例如<code><span class="token property">CopyOnWriteList</span></code>,
                        <code><span class="token property">Vector</span></code>等都是代替数组的线程安全的类，用于不同的场景。例如:</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Vector</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
nums<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
nums<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">456</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">toast</span><span class="token punctuation">(</span><span class="token string">&quot;长度为&quot;</span> <span class="token operator">+</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">toast</span><span class="token punctuation">(</span><span class="token string">&quot;第一个元素为&quot;</span> <span class="token operator">+</span> nums<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>但很明显的是，这些类不像数组那样简便易用，也不能使用诸如<code><span class="token function">slice</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>之类的方便的函数。在未来可能会加入线程安全的数组来解决这个问题。当然您也可以为每个数组的操作加锁来解决线程安全问题：
                    </p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> nums <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> numsLock <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//向数组添加元素123</span>
    numsLock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    nums<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;线程: %s, 数组: %s&quot;</span><span class="token punctuation">,</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nums<span class="token punctuation">)</span><span class="token punctuation">;</span>
    numsLock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//向数组添加元素456</span>
    numsLock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    nums<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">456</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;线程: %s, 数组: %s&quot;</span><span class="token punctuation">,</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nums<span class="token punctuation">)</span><span class="token punctuation">;</span>
    numsLock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//删除数组最后一个元素</span>
numsLock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
nums<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;线程: %s, 数组: %s&quot;</span><span class="token punctuation">,</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nums<span class="token punctuation">)</span><span class="token punctuation">;</span>
numsLock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <h2 id="sync-func" tabindex="-1"><a class="header-anchor" href="#sync-func" aria-hidden="true">#</a>
                        sync(func)</h2>
                    <ul>
                        <li><code><span class="token property">func</span></code> {Function} 函数</li>
                        <li>返回 {Function}</li>
                    </ul>
                    <p>给函数func加上同步锁并作为一个新函数返回。</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token parameter">x</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    i <span class="token operator">+=</span> x<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> syncAdd <span class="token operator">=</span> <span class="token function">sync</span><span class="token punctuation">(</span>add<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">syncAdd</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">toast</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <h1 id="线程通信" tabindex="-1"><a class="header-anchor" href="#线程通信" aria-hidden="true">#</a> 线程通信</h1>
                    <p>Auto.js提供了一些简单的设施来支持简单的线程通信。<code>threads<span class="token punctuation">.</span><span class="token function">disposable</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>用于一个线程等待另一个线程的(一次性)结果，同时<code>Lock<span class="token punctuation">.</span><span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>提供了Condition对象用于一般的线程通信(await,
                        signal)。另外，<code><span class="token property">events</span></code>模块也可以用于线程通信，通过指定<code><span class="token property">EventEmitter</span></code>的回调执行的线程来实现。
                    </p>
                    <p>使用<code>threads<span class="token punctuation">.</span><span class="token function">disposable</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>可以简单地等待和获取某个线程的执行结果。例如要等待某个线程计算&quot;1+.....+10000&quot;:
                    </p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token keyword">var</span> sum <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">disposable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//启动子线程计算</span>
threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token comment">//从1加到10000</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        s <span class="token operator">+=</span> i<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//通知主线程接收结果</span>
    sum<span class="token punctuation">.</span><span class="token function">setAndNotify</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//blockedGet()用于等待结果</span>
<span class="token function">toast</span><span class="token punctuation">(</span><span class="token string">&quot;sum = &quot;</span> <span class="token operator">+</span> sum<span class="token punctuation">.</span><span class="token function">blockedGet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>如果上述代码用<code><span class="token property">Condition</span></code>实现：</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token comment">//新建一个锁</span>
<span class="token keyword">var</span> lock <span class="token operator">=</span> threads<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//新建一个条件，即&quot;计算完成&quot;</span>
<span class="token keyword">var</span> complete <span class="token operator">=</span> lock<span class="token punctuation">.</span><span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//从1加到10000</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        sum <span class="token operator">+=</span> i<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//通知主线程接收结果</span>
    lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    complete<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//等待计算完成</span>
lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
complete<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//打印结果</span>
<span class="token function">toast</span><span class="token punctuation">(</span><span class="token string">&quot;sum = &quot;</span> <span class="token operator">+</span> sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>如果上诉代码用<code><span class="token property">events</span></code>模块实现：</p>
                    <div class="language-javascript ext-js line-numbers-mode">
                        <pre class="language-javascript"><code><span class="token comment">//新建一个emitter, 并指定回调执行的线程为当前线程</span>
<span class="token keyword">var</span> sum <span class="token operator">=</span> events<span class="token punctuation">.</span><span class="token function">emitter</span><span class="token punctuation">(</span>threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
threads<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token comment">//从1加到10000</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        s <span class="token operator">+=</span> i<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//发送事件result通知主线程接收结果</span>
    sum<span class="token punctuation">.</span><span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">&#39;result&#39;</span><span class="token punctuation">,</span> s<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sum<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&#39;result&#39;</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">s</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">toastLog</span><span class="token punctuation">(</span><span class="token string">&quot;sum = &quot;</span> <span class="token operator">+</span> s <span class="token operator">+</span> <span class="token string">&quot;, 当前线程: &quot;</span> <span class="token operator">+</span> threads<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
                        <div class="line-numbers" aria-hidden="true">
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                            <div class="line-number"></div>
                        </div>
                    </div>
                    <p>有关线程的其他问题，例如生产者消费者等问题，请用Java相关方法解决，例如<code>java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>concurrent<span class="token punctuation">.</span>BlockingQueue</code>。
                    </p>
                </div><!---->
                <footer class="page-meta"><!---->
                    <div class="meta-item update-time"><span class="label">上次编辑于: </span><!----></div>
                    <div class="meta-item contributors"><span class="label">贡献者: </span><!--[--><!--[--><span
                            class="contributor" title="email: 946994919@qq.com">hyb1996</span><!--]--><!--]--></div>
                </footer>
                <nav class="page-nav"><a href="settings.html" class="nav-link prev" aria-label="settings - 设置">
                        <div class="hint"><span class="arrow left"></span>上一页</div>
                        <div class="link"><!---->settings - 设置</div>
                    </a><a href="timers.html" class="nav-link next" aria-label="timers - 定时器">
                        <div class="hint">下一页<span class="arrow right"></span></div>
                        <div class="link">timers - 定时器<!----></div>
                    </a></nav><!----><!----><!--]-->
            </main><!--]--><!----><!--]-->
        </div><!--]--><!----><!----><!--]-->
    </div>
    <script type="module" src="../../assets/app.6aa2b576.js" defer></script>
</body>

</html>